data<-read.table("/data2/bsi/RandD/bora/OV_EQTL/AFFY_ILL_MERGED/eQTL_results_R_cis_OV.txt",stringsAsFactors =F,sep="\t",head=T)
mergedarray<-c()
id<-paste(data$SNP,data$gene,sep="_")
mergedarray<-cbind(id,data$p.value)
colnames(mergedarray)<-c("ID","PVAL")
d<-"/data2/bsi/RandD/bora/OV_EQTL/AFFY_ILL_MERGED/results_permutation_sorted_pval/"
samplefiles<-list.files(d)
i<-1
while(i<101)
{
	print(i)
	file<-paste(d, samplefiles[i],sep='/')
	data<-c()
	data<-read.table(file,sep="\t",head=T,stringsAsFactors =F)
	id<-paste(data$SNP,data$gene,sep="_")
	mergedarray1<-c()
	mergedarray1<-cbind(id,data$p.value)
	colnames(mergedarray1)<-c("ID",samplefiles[i])
	mergedarray<-merge(mergedarray,mergedarray1,by="ID",all.x=T)
	i<-i+1
}
write.table(mergedarray,"/data2/bsi/RandD/bora/OV_EQTL/AFFY_ILL_MERGED/Merged_PVAL_temp.txt",sep="\t",quote=F,row.names=F,col.names=T)
CountMySignificant <- function(x)
{  
              count = 0   
              for(i in 3:length(x))   
    {  
        if(x[i] < x[2] && !is.na(x[i]))  
        {  
              count = count + 1  
        }  
     }  
     return(count)  
}
CountNA<-function(x)
{
		return(sum ( is.na (x)))
}
colnames<-colnames(mergedarray)
NA_count<-apply(mergedarray,1,CountNA)
Count<-apply(mergedarray,1,CountMySignificant)
mergedarray<-cbind(mergedarray,NA_count,Count)
colnames(mergedarray)<-c(colnames,"NA_COUNT","PVALCOUNT")
write.table(mergedarray,"/data2/bsi/RandD/bora/OV_EQTL/AFFY_ILL_MERGED/Merged_PVAL.txt",sep="\t",quote=F,row.names=F,col.names=T)
